package com.bjdd.jdy.ui.dealRecord;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bjdd.jdy.R;
import com.bjdd.jdy.adapter.DealRecordAdapter;
import com.bjdd.jdy.base.BaseActivity;
import com.bjdd.jdy.entity.DealRecord;
import com.bjdd.jdy.entity.result.AcountResult;
import com.bjdd.jdy.util.RecyclerViewDivider;
import com.bjdd.jdy.util.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import in.srain.cube.views.ptr.PtrClassicDefaultFooter;
import in.srain.cube.views.ptr.PtrClassicDefaultHeader;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

public class DealRecordActivity extends BaseActivity<DealRecordPresenter, DealRecordModel> implements DealRecordContract.View {

    @Bind(R.id.toolbar_back)
    ImageView toolbarBack;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.rcv)
    RecyclerView rcv;
    @Bind(R.id.refreshlayout)
    PtrFrameLayout refreshlayout;
    private PtrClassicDefaultFooter footer;
    private boolean isLoadMore = false;
    private List<DealRecord> list;
    private DealRecordAdapter adapter;
    private int page = 0;
    private int size = 10;
    @Override
    public int getLayoutId() {
        return R.layout.activity_deal_record;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this,getResources().getColor(R.color.toolbar_bg),0);
        title.setText("交易记录");
        rcv.setLayoutManager(new LinearLayoutManager(context));
        PtrClassicDefaultHeader header = new PtrClassicDefaultHeader(context);
        header.onUIRefreshBegin(refreshlayout);
        refreshlayout.setHeaderView(header);
        refreshlayout.addPtrUIHandler(header);
        footer = new PtrClassicDefaultFooter(context);
        footer.onUIRefreshBegin(refreshlayout);
        refreshlayout.setFooterView(footer);
        refreshlayout.addPtrUIHandler(footer);
        refreshlayout.setLoadingMinTime(1000);
        refreshlayout.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                isLoadMore = true;
                page ++;
                presenter.getAcount(page,size);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                isLoadMore = false;
                page = 0;
                presenter.getAcount(page,size);
            }
        });
        list = new ArrayList<>();
        RecyclerViewDivider divider = new RecyclerViewDivider(context,LinearLayoutManager.VERTICAL,2, ContextCompat.getColor(context,R.color.divider_bg));
        rcv.addItemDecoration(divider);
        adapter = new DealRecordAdapter(context,list);
        rcv.setAdapter(adapter);
        presenter.getAcount(page,size);
    }

    @OnClick(R.id.toolbar_back)
    public void onClick(){
        finish();
    }

    @Override
    public void acountSuccess(AcountResult data) {
        if (isLoadMore){
            //加载更多
            list.addAll(data.getContent());
            adapter.notifyDataSetChanged();
        }else {
            list.clear();
            list.addAll(data.getContent());
            adapter.notifyDataSetChanged();
        }
        refreshlayout.refreshComplete();
    }
}
